package com.qzdsoft.erpcloud.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.domain.sys.SysTableAuth;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysTableAuthService;
import com.qzdsoft.erpcloud.vo.sys.SysTableAuthInfo;
import com.qzdsoft.erpcloud.vo.sys.SysTableAuthQueryParam;
import com.qzdsoft.utils.RequestUtil;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.utils.constant.SystemConstant;
import com.qzdsoft.utils.enums.CommonSfEnum;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.sys.SysRoleStatus;
import com.qzdsoft.vo.sys.SysTableAuthFieldTypeEnum;

/**
 * Created by Admin
 */
@Controller
@RequestMapping("/sys/table/auth")
public class SysTableAuthController {
	private static final Logger logger = LoggerFactory.getLogger(SysTableAuthController.class);
	private static final String BASEPATH = "sys/table";
    @Autowired
    private SysTableAuthService sysTableAuthService;

    /**
     *
     * @param request
     * @param model
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {
//        List<EnumPo> sfList = new ArrayList<EnumPo>();
//        Map<Object, Object> sfMap = new HashMap<Object, Object>();
//        for(CommonSfEnum e : CommonSfEnum.values()){
//            SysUtils.enums(sfList, sfMap, e.getKey(), e.getVal());
//        }
//
//        List<EnumPo> fieldTypeList = new ArrayList<EnumPo>();
//        Map<Object, Object> fieldTypeMap = new HashMap<Object, Object>();
//        for(SysTableAuthFieldTypeEnum e : SysTableAuthFieldTypeEnum.values()){
//            SysUtils.enums(fieldTypeList, fieldTypeMap, e.getKey(), e.getVal());
//        }
//
//        model.addAttribute("sfList", sfList);
//        model.addAttribute("sfMap", sfMap);
//
//        model.addAttribute("fieldTypeList", fieldTypeList);
//        model.addAttribute("fieldTypeMap", fieldTypeMap);


        model.addAttribute("commonStatus2List", SysRoleStatus.ALL);
        return BASEPATH+"/index";
    }
    /**
     * 页面查询
     * @param request
     * @param info
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse<SysTableAuthInfo> data(HttpServletRequest request, SysTableAuthQueryParam info) {
    	if(info.isFirst()) {
    		return LayTableResponse.nullDataPage();
    	}
        return sysTableAuthService.selectData(info);
    }
    /**
     * 打开新增页面
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public ModelAndView addinit() {
    	ModelAndView mv = new ModelAndView();
    	mv.addObject("commonStatus2List", SysRoleStatus.ALL);
    	mv.addObject("super", sysTableAuthService.selectAllAuper());
    	mv.setViewName(BASEPATH+"/add");
    	return mv;
    }
    
    /**
     * 新增保存
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="表格权限新增保存")
    @ResponseBody
    @RequestMapping("/add")
    public Response add(SysTableAuth info) {
    	
    	return sysTableAuthService.saveOrUpd(info);
    }
    
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(Integer id) {
    	ModelAndView mv = new ModelAndView();
    	mv.addObject("super", sysTableAuthService.selectAllAuper());
    	mv.addObject("commonStatus2List", SysRoleStatus.ALL);
    	mv.addObject("obj",sysTableAuthService.selectById(id));
    	mv.setViewName(BASEPATH+"/edit");
        return mv;
    }
    
    /**
     * 表格权限修改保存
     * @param info
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="表格权限修改保存")
    @ResponseBody
    @RequestMapping("/edit_execute")
    public Object editExecute(SysTableAuth info) {
       return sysTableAuthService.saveOrUpd(info);
    }
    
    /**
     * 删除
     * @param id
     * @return
     */
    @Permission(validate=false)
    @SysLog(description="表格权限删除")
    @ResponseBody
    @RequestMapping("/del_execute")
    public Object delExecute(Integer id) {
        return sysTableAuthService.delete(id);
    }

   
    
}
